# 加载用到的库
from cgitb import reset
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris #仅用于加载数据集
import pandas as pd
import seaborn as sns
# import scipy.stats as ss

def heatMap(data):
    fig, ax = plt.subplots(figsize=(10, 10))
    ax = sns.heatmap(
                data, 
                linewidths=0.1, # 设置每个单元方块的间隔
                
                annot=True, # 是否在颜色格子上显示数值
                
                vmax=1, vmin=0, # 数值范围
                xticklabels=['Calyx length', 'Calyx width', 'Petal length', 'Petal width'], # 是否显示X轴
                yticklabels=['Calyx length', 'Calyx width', 'Petal length', 'Petal width'], # 是否显示Y轴
                square=True, 
                
                cmap="YlGnBu" # 指定填充色
    )
    ax.set_title(' Heat Map ', fontsize=18)
    ax.set_ylabel('Y', fontsize=18)
    ax.set_xlabel('X', fontsize=18)
    # plt.savefig('Random.png') # 保存图片

iris = load_iris()
X=iris.data[:, :4] # 取前四列数据
print('X:\n',X)
result_n = np.corrcoef(X, rowvar=False) # rowvar为True则行为变量，False则列为变量
df = pd.DataFrame(X)
sns.pairplot(df)
# pd.plotting.scatter_matrix(df, figsize=(12,12),range_padding=0.5) # 与上面那行效果一样，只是没那么美观

# Pearson相关系数（皮尔逊相关系数）
result_pearson = df.corr().abs()
print(result_pearson)
heatMap(result_pearson)

# Sperman秩相关系数（斯皮尔曼相关系数）
result_sperman = df.corr(method='spearman').abs()
print(result_sperman)
heatMap(result_sperman)

plt.show()